package com.example.carbon.controller;

import com.example.carbon.pojo.dto.PageLimit;
import com.example.carbon.pojo.dto.ProduceModelDtoInsert;
import com.example.carbon.pojo.vo.RestBean;
import com.example.carbon.server.info.MaterialInfoServer;
import com.example.carbon.server.info.MaterialProductServer;
import com.example.carbon.server.info.ProductModelServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/produceModel")
@Slf4j
public class ProductModelController {
    @Resource
    ProductModelServer productModelServer;

    @Resource
    MaterialInfoServer materialInfoServer;

    /**
     * 获取数据
     * @param page
     * @param item
     * @return
     */
    @RequestMapping("/get/{page}/{item}")
    public RestBean selectAllProductData(@PathVariable(value = "page") int page,
                                         @PathVariable(value = "item") int item,
                                         String modelName){
        try {
            PageLimit pageLimit=new PageLimit(page,item);
            return RestBean.success(productModelServer.selectAllInfo(pageLimit,modelName));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure("获取数据失败");
        }
    }

    /**
     * 根据id返回前端修改可能涉及的数据
     * @param id
     * @return
     */
    @RequestMapping("/get/info/{id}")
    public RestBean selectInfoById(@PathVariable("id")int id){
        try {
            return RestBean.success(productModelServer.selectInfoById(id));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure("获取该模型信息失败");
        }
    }

    @RequestMapping("/update/{id}")
    public RestBean updateNewInfo(@RequestBody ProduceModelDtoInsert produceModelDtoInsert,@PathVariable(value = "id") int id){
        try {
            return RestBean.success(materialInfoServer.updateNewInfo(produceModelDtoInsert,id));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure("更新失败");
        }
    }

    @RequestMapping("/getAllType")
    public RestBean getAllModelType(){
        try {
            return RestBean.success(productModelServer.selectAllModelType());
        }catch (Exception e){
            return RestBean.failure("获取数据失败");
        }
    }
}
